import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime

class LikeShopTest(): 
    def __init__(self) -> None:
        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)


    def test(self):
        self.browser = webdriver.Firefox()
        self.browser.get("http://likeshop.abc/admin/account/login.html")
        sleep(3)
        self.browser.find_element(By.NAME, 'account').send_keys('ljy')
        self.browser.find_element(By.NAME, 'password').send_keys('162433')
        self.browser.find_element(By.NAME, 'code').send_keys('1111')
        self.browser.find_element(By.ID, 'login').click()
        sleep(3)
        self.browser.find_element(By.LINK_TEXT, '商品').click()

        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(frame1)
        sleep(1)
        self.test_shop_add()
        sleep(1)
        
        # self.browser.switch_to.default_content()
        # logout_button = self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite')
        # ActionChains(self.browser).move_to_element(logout_button).perform()
        # sleep(1)
        # self.simple_click('/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a')
        # sleep(1)
    
    def test_shop_list(self):
        self.browser.find_element(By.XPATH, '//*[@id="keyword"]').send_keys('文具')
        sleep(1)
        self.query_list('文具') 
        sleep(1)
        self.simple_click('/html/body/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/input')
        sleep(1)
        self.simple_click('/html/body/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/dl/dd[3]')
        self.query_list('晨光')

    def query_list(self, query_name):
        self.simple_click('/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]')
        sleep(2)
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        count = len(shop_list)
        if count == 0:
            print('没有数据')
        else:
            shop_name_ret = True
            for shop_item in shop_list:
                shop_name = shop_item.find_element(By.XPATH, './td[2]/div').text
                if query_name not in shop_name:
                        shop_name_ret = False
                # for i in range(1, count + 1):
                #     xpath = '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[%s]/td[2]/div' %i
                #     shop_name = self.browser.find_element(By.XPATH, xpath).text
                #     if '文具' not in shop_name:
                #         shop_name_ret = False
                if shop_name_ret:
                    print('没有问题')
                else:
                    print('有bug') 

    def test_shop_add(self):
        # 点击发布商评
        self.simple_click('/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]')
        sleep(1)
        # 进入商品发布子框架
        shop_add_framel = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(shop_add_framel)

        new = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        # 定位商品名称输入框并输入商品名称
        self.simple_keys('/html/body/div/div/div[1]/div/div/div[1]/div/input[2]', '梁君余的new shopname' + new)
        sleep(1)

        # 商品分类
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]')

        # 添加图片
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a')
        sleep(1) 
        # 进入图片上传子框架
        upload_img_framel = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(upload_img_framel)
        self.test_upload_img('C:\\Users\\27477\\Desktop\\新建文件夹\\wj.jpg')

        # 轮播图添加，回到主框架
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_framel)
        # 点击商品轮播图 图片上传
        self.simple_click('/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a')
        
        sleep(1)
        # 进入上传轮播图 图片上传子框架
        upload_img_frame2 = self.browser.find_element(By.XPATH, '/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_frame2)
        self.test_upload_img('C:\\Users\\27477\\Desktop\\新建文件夹\\wj.jpg')
 
        # 回到主框架 库存填写
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_framel)
        # 价格库存
        self.simple_click('/html/body/div[1]/ul/li[2]')
        sleep(1)
        # 填写库存信息
        self.simple_keys('/html/body/div[1]/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input', '1')
        self.simple_keys('/html/body/div[1]/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input', '1')
        self.simple_keys('/html/body/div[1]/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input', '2')
        self.simple_keys('/html/body/div[1]/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input', '50')
        sleep(1)
        # 回到主框架
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        sleep(1)
        # 保存退出
        self.browser.find_element(By.LINK_TEXT, '保存').click()
        sleep(2)

        # 上架
        self.simple_click('/html/body/div/div/div[2]/ul/li[2]')
        sleep(1)

        # 获取商品列表                                    
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(shop_list) != 0:
            for shop_item in shop_list:
                self.update_test(new)
                self.simple_click('/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]')
                sleep(2)
                self.browser.find_element(By.LINK_TEXT, '确定').click()
                sleep(3)

    def test_upload_img(self, file_path):
        # 提交文件
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div[1]/input').send_keys(file_path)
        sleep(4)
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/ul/li[1]/div/img').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()

    def update_test(self, new):
        # 编辑
        self.simple_click('/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[1]')
        sleep(2)
        frame3 = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame3)
        sleep(1)
        # 修改商品名称
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
        sleep(1)
        self.simple_keys('/html/body/div/div/div[1]/div/div/div[1]/div/input[2]', '梁君余修改后的shopname' + new)
        sleep(1)
        # 商品分类
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[5]')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input')
        sleep(1)
        self.simple_click('/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]')
        sleep(1)
        # 修改库存价格
        self.simple_click('/html/body/div[1]/ul/li[2]')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').clear()
        sleep(1)
        self.simple_keys('//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input', '4')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').clear()
        sleep(1)
        self.simple_keys('//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input', '3')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').clear()
        sleep(1)
        self.simple_keys('//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input', '2.5')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').clear()
        sleep(1)
        self.simple_keys('//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input', '100')
        sleep(1)
        self.simple_click('/html/body/div/ul/li[1]')
        sleep(1)
        # 回到主框架
        self.browser.switch_to.default_content()
        frame1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(frame1)
        sleep(1)
        # 保存退出
        self.browser.find_element(By.LINK_TEXT, '保存').click()
        sleep(2)
        

    def simple_click(self, e):
        self.browser.find_element(By.XPATH, e).click()
    
    def simple_keys(self, xpath, text):
        self.browser.find_element(By.XPATH, xpath).send_keys(text)

likeshop = LikeShopTest() 
likeshop.test()